package com.fp.m7.client.view;

import java.util.Date;

import org.gwttime.time.DateTime;

import com.fp.m7.client.MyMessages;
import com.fp.m7.client.common.Utils;
import com.fp.m7.client.place.ProfilePlace;
import com.fp.m7.client.presenter.CommentPresenter;
import com.fp.m7.shared.Constants;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * A view of a single posted comment.
 * 
 * @author 7
 */
public class CommentView extends Composite implements CommentPresenter.Display {

    private static MyMessages msg = GWT.create(MyMessages.class);

    private Image imgProfile;
    private Label lblContents;
    private Button btnDelete;
    private Hyperlink hprlnkAuthorname;
    private HTML dateLabel;

    public CommentView() {
        DecoratorPanel decorator = new DecoratorPanel();
        decorator.addStyleName("commentWidget");
        initWidget(decorator);

        DockPanel dockPanel = new DockPanel();
        dockPanel.setHeight("100px");
        decorator.add(dockPanel);

        imgProfile = new Image(Constants.DEFAULT_AVATAR_URL);
        imgProfile.setSize("100px", "100px");
        dockPanel.add(imgProfile, DockPanel.WEST);

        btnDelete = new Button();
        btnDelete.setVisible(false); // not visible by default
        btnDelete.setStyleName("buttonDelete");
        dockPanel.add(btnDelete, DockPanel.EAST);

        VerticalPanel verticalPanel = new VerticalPanel();
        dockPanel.add(verticalPanel, DockPanel.CENTER);

        hprlnkAuthorname = new Hyperlink("", false, ProfilePlace.NAME);
        hprlnkAuthorname.setHeight("20px");
        hprlnkAuthorname.addStyleName("withHorizontalPadding");
        verticalPanel.add(hprlnkAuthorname);

        lblContents = new Label("");
        lblContents.setSize("300px", "60px");
        verticalPanel.add(lblContents);

        dateLabel = new HTML();
        dateLabel.setHeight("20px");
        dateLabel.addStyleName("withHorizontalPadding");
        verticalPanel.add(dateLabel);
    }

    public void setContents(String contents, Date date, String authorNickname, String avatarUrl, String profileToken) {
        lblContents.setText(contents);

        dateLabel.setHTML(msg.posted() + Utils.htmlForATimeAgoElement(new DateTime(date)));
        Utils.initJQueryTimeAgo();

        hprlnkAuthorname.setText(authorNickname);
        hprlnkAuthorname.setTargetHistoryToken(profileToken);

        imgProfile.setUrl(avatarUrl == null ? Constants.DEFAULT_AVATAR_URL : avatarUrl);
    }

    public void showDeleteButton(boolean mode) {
        btnDelete.setVisible(mode);
    }

    public HasClickHandlers getProfileButton() {
        return imgProfile;
    }

    public HasClickHandlers getDeleteButton() {
        return btnDelete;
    }

    public HasText getContents() {
        return lblContents;
    }
}
